/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseFilePanel.java
 *
 * Created on Sep 1, 2012, 6:55:49 PM
 */
package view.base.files;

import java.awt.Window;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Asia
 */
public class ChooseFilePanel extends javax.swing.JPanel {

    /**
     * selected file
     */
    protected File file = null;
    private Window parent = null;
    public final static Logger EXCEPTIONLOGGER = Logger.getLogger("exception logger");

    /**
     * Creates new file ChooseFilePanel
     */
    public ChooseFilePanel() {
        initComponents();
        this.setVisible(true);
    }

    protected void action() {
    }

    protected void setButtonName(String name) {
        jFileChooser1.setApproveButtonText(name);
    }

    public void setParent(Window jp) {
        this.parent = jp;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();

        jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooser1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooser1ActionPerformed
        if (evt.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)) {
            // CANCEL BUTTON
            this.setVisible(false);
        } else if (evt.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
            // ACTION BUTTTON
            file = jFileChooser1.getSelectedFile();
            action();
            this.setVisible(false);
        }

        if (this.parent != null) {
            this.parent.setVisible(false);
            this.parent.dispose();
        }
    }//GEN-LAST:event_jFileChooser1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jFileChooser1;
    // End of variables declaration//GEN-END:variables
    
    /**
     * zapisuje Stringa do pliku
     * @param text zapisywany tekst
     * @param fileName wybrany plik
     */
    public static void write(String text, File fileName){
        try {
            FileWriter file = new FileWriter(fileName);
            BufferedWriter buffer = new BufferedWriter(file);
            PrintWriter out = new PrintWriter(buffer);
            out.print(text);
            out.close();
            buffer.close();
            file.close();
        } catch (IOException e) {
            String message="exception "+e.getMessage();
            EXCEPTIONLOGGER.finest(message);
            JOptionPane.showMessageDialog(null, "Problem occurred\n"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * wczytuje zawartość tekstową pliku
     * @param FileName wybrany plik
     * @return zawartość pliku - String
     */
    public static String open(File FileName) {
        BufferedReader br;
        String all = "";
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader(FileName));
            while ((sCurrentLine = br.readLine()) != null) {
                all = all + sCurrentLine + "\n";
            }
            br.close();
        } catch (IOException e) {
            String message="exception "+e.getMessage();
            EXCEPTIONLOGGER.finest(message);
            JOptionPane.showMessageDialog(null, "Problem occurred\n"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return all;
    }
}
